var admin_collection = db.collection("admin");
var md5 = require("md5");
(function () {
    var _con = function (info) {
        var self = this;
        // console.log(self);
        if (info) {
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 数据类型
            self.type = "admin";
            // 自有id,a_+2位随机数+当前时间的毫秒数转16进制 
            self.id = "a_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 登陆用户名
            self.username = info.username;
            // MD5加密密码
            self.password = md5(info.password || "123456");
            // 手机号
            self.mobile = info.mobile;
            // 邮箱
            self.email = info.email;
            // 管理员等级
            self.level = info.level || "normal";
        }
        // console.log(this);
    };
    // 导出类
    module.exports = _con;
    // 添加管理员
    _con.prototype.save = co.wrap(function* (next) {
        var self = this;
        var _a = self.username ? yield mongo.findOne(admin_collection, {
            query: {
                username: self.username
            }
        }) : null;
        _a ? true : self.mobile ? _a = yield mongo.findOne(admin_collection, {
            query: {
                mobile: self.mobile
            }
        }) : null;
        _a ? true : self.email ? _a = yield mongo.findOne(admin_collection, {
            query: {
                email: self.email
            }
        }) : null;
        console.log(_a)
        if (_a) {
            return printError("此用户已存在")
        };
        var _a = yield mongo.insertOne(admin_collection, self);
        return yield mongo.findOne(admin_collection, {
            query: {
                username: self.username,
                mobile: self.mobile,
                email: self.email
            }
        });
    });
    // 管理员登陆
    _con.prototype.login = co.wrap(function* (info) {
        // console.log(info);
        printData("管理员登陆：", info);
        var pwd = info.password;
        delete info.password;
        var doc = yield mongo.findOne(admin_collection, {
            query: info
        });
        if (!doc) {
            return printError("找不到管理员，请检查 用户名/手机号/邮箱 是否正确");
        }
        if (doc.password === md5(pwd)) {
            // 更新登陆时间
            var _time = (new Date()).getTime();
            var doc = yield mongo.findOneAndUpdateByObjectId(admin_collection, {
                _id: doc._id,
                data: {
                    // 最新一次登陆时间
                    newlogintime: _time,
                    // 上次登陆时间
                    logintime: doc.newlogintime || _time
                }
            });
            delete doc.value.password;
            // console.log("doc:", doc)
            return doc.value;
        } else {
            return printError("密码错误");
        };
    });
    // 更新管理员信息
    _con.prototype.update = co.wrap(function* (info) {
        printData("开始更新管理员信息：", info);
        var _id = info._id;
        delete info._id;
        delete info.password;
        delete info.verifycode;
        var doc = yield mongo.findOneAndUpdateByObjectId(admin_collection, {
            _id: _id,
            data: info
        });
        if (!doc) {
            return printError("查找不到此管理员");
        };
        delete doc.value.password;
        return doc.value;
    });
    // 修改管理员密码
    _con.prototype.changepassword = co.wrap(function* (info) {
        printData("正在修改管理员密码：", info);
        var doc = yield mongo.findOneByObjectId(admin_collection, info._id);
        if (!(doc.password == md5(info.oldpassword))) {
            return printError("旧密码填写不正确");
        };
        yield mongo.updateOneByObjectId(admin_collection, {
            _id: info._id,
            data: {
                password: md5(info.newpassword)
            }
        });
        return {
            status: "ok",
            toString: "密码修改成功"
        };
    });
} ());